//
//	 ______    ______    ______    
//	/\  __ \  /\  ___\  /\  ___\   
//	\ \  __<  \ \  __\_ \ \  __\_ 
//	 \ \_____\ \ \_____\ \ \_____\ 
//	  \/_____/  \/_____/  \/_____/ 
//
//	Powered by BeeFramework
//
//
//  DecorateBoard.m
//  Pizza
//
//  Created by sendInfo on 2015-04-02.
//  Copyright (c) 2015年 sendInfo. All rights reserved.
//

#import "DecorateBoard.h"

#pragma mark -

@implementation TemplateHeadBtn

SUPPORT_AUTOMATIC_LAYOUT( YES )
SUPPORT_RESOURCE_LOADING( YES )
- (void)dataDidChanged
{
    
    if ([self.data isKindOfClass:[MyStyle class]]) {
        
        MyStyle *gr = self.data;
        $(@"title").DATA(gr.title);
        
    }else{
    
        $(@"title").DATA(self.data);
    }
    
    
}


@end


@implementation TemplateItemCell

SUPPORT_AUTOMATIC_LAYOUT( YES )
SUPPORT_RESOURCE_LOADING( YES )

- (void)dataDidChanged
{

    if ([self.data isKindOfClass:[MyStyle class]]) {
       
        MyStyle *gr = self.data;
        NSString *url = [NSString stringWithFormat:@"%@/%@",[HttpRequest sharedInstance].imageUrl,gr.pic];
        $(@"template").DATA(url);
        $(@"selected").DATA(nil);
        
    }else{
    
        MyStyle *gr = self.data;
        NSString *url = [NSString stringWithFormat:@"%@/%@",[HttpRequest sharedInstance].imageUrl,gr.pic];
        $(@"template").DATA(url);
        $(@"selected").DATA(nil);
    }

}

@end


@interface ShopTemplateCell(){

    NSString *_type;

    NSDictionary *dataD;
    NSArray *myImages;
    BeeUIImageView *_lastImg;
    BOOL _isOrNot;
    NSArray *_titles;
    
    NSString *myStyleUrl;
    
}

@end

@implementation ShopTemplateCell

SUPPORT_AUTOMATIC_LAYOUT( YES )
SUPPORT_RESOURCE_LOADING( YES )
DEF_SIGNAL(UPDATESUCCESS)

- (void)load
{

    //item
//     UIImage *smal = [UIImage imageFromString:@"small-template.jpg"];
//     UIImage *big = [UIImage imageFromString:@"big-template.jpg"];
//     UIImage *one = [UIImage imageFromString:@"one-template.jpg"];
//     UIImage *two = [UIImage imageFromString:@"two-template.jpg"];
//     UIImage *three = [UIImage imageFromString:@"three-template.jpg"];
//     UIImage *four = [UIImage imageFromString:@"four-template.jpg"];
//     UIImage *five = [UIImage imageFromString:@"five-template.jpg"];
//    
//    dataD = @{@"小店":@[smal],@"大店":@[big],@"一星":@[one],@"二星":@[two],@"三星":@[three],@"四星":@[four],@"五星":@[five]};
//    myImages = @[one,two,five,four];
    
    self.list.lineCount = 3;
    self.list.animationDuration = 0.25f;
    self.list.whenReloading = ^{
    
        NSArray *grades = self.data[@"grades"];
        self.list.total = 1;
        for (BeeUIScrollItem *item in self.list.items) {
            
            item.size = CGSizeMake(self.list.size.width/3, self.list.size.width/3);
            item.data = [grades firstObject];
            item.clazz = [ TemplateItemCell class];
        }
        
        if ([_type intValue] == 1) {
            
            self.list.total = grades.count;
            for (BeeUIScrollItem *item in self.list.items) {
                
                item.size = CGSizeMake(self.list.size.width/3, self.list.size.width/3);
                item.data = [grades safeObjectAtIndex:item.index];
                item.clazz = [ TemplateItemCell class];
            }
            
        }
    
    };
    
    //分类按钮
    self.menulist.lineCount = 1;
    self.menulist.animationDuration = 0.25f;
    self.menulist.whenReloading = ^{
    
        if ([_type intValue] == 0) {
            
            //NSArray *arr = @[@"小店",@"大店",@"一星",@"二星",@"三星",@"四星",@"五星"];
            //_titles = arr;
            
            self.menulist.total = _titles.count;
            
            for (BeeUIScrollItem *item in self.menulist.items) {
                
                item.size = CGSizeMake(55, 35);
                item.data = [_titles safeObjectAtIndex:item.index];
                item.clazz = [TemplateHeadBtn class];
              
            }
            
        }else{
        
            self.menulist.total = 1;
            BeeUIScrollItem *item  = self.menulist.items[0];
            item.size = CGSizeMake(self.menulist.size.width, 35);
            item.data = @"我的风格店铺";
            item.clazz = [TemplateHeadBtn class];
        
        }
        
    };
    


}

- (void)dataDidChanged
{


    NSDictionary *dic = self.data;
    if ([dic[@"type"] isEqualToString:@"0"]) {
        
        $(@"title").DATA(@"装修店铺");
        
        if (_titles==nil) {
          
            _titles = dic[@"grades"];
        }
        
      _type = @"0";

        
    }else{
        
        $(@"title").DATA(@"我的风格");
        _type = @"1";
      
    }
    
    [self.list reloadData];
    [self.menulist reloadData];
}

ON_SIGNAL3(TemplateHeadBtn, mask, signal)
{

    _isOrNot = NO;
    TemplateHeadBtn *cell = (TemplateHeadBtn*)signal.sourceCell;
    
   
    
    if ([self.data[@"type"] isEqualToString:@"0"]) {
        
        self.data = @{@"type":@"0",@"grades":@[cell.data]};
       
        
        
    }else{
    
    
         self.data = @{@"type":@"1",@"grades":self.data[@"grades"]};
       
    }
    
    [self.list reloadData];
    [self.menulist reloadData];
    
    
    //++

}

ON_SIGNAL3(TemplateItemCell, mask, signal)
{

    TemplateItemCell *cell = (TemplateItemCell *)signal.sourceCell;
    BeeUIImageView *imgv = (BeeUIImageView*)$(cell).FIND(@"selected").view;
    imgv.image = [UIImage imageFromString:@"duiicon"];
    
    if (imgv != _lastImg) {
        
        _lastImg.image = nil;
        _isOrNot = NO;
        
    }
    
    if (!_isOrNot) {
        
         imgv.image = [UIImage imageFromString:@"duiicon"];
        
    }else{
         imgv.image = nil;
    }
    _lastImg = imgv;
    _isOrNot = !_isOrNot;
    
    NSString *styid;
    NSString *urlPath;
    if ([cell.data isKindOfClass:[ShopGrade class]]) {
        
        ShopGrade *sg = cell.data;
        styid = sg.id;
        urlPath = sg.pic;
    }else{
    
        MyStyle *st = cell.data;
        styid = st.id;
        urlPath = st.pic;
    }
    myStyleUrl = urlPath;
    self.MSG(HttpAPI.style_update).INPUT(@"body",@{@"token":[SendIFAPPDefault shareAppDefault].userToken,@"source":@"IOS",@"styleId":styid});
    
    
    

}

ON_MESSAGE3(HttpAPI, style_update, msg)
{

    if (msg.sending) {
        
        [self presentLoadingTips:@"设置风格"];
        
    }else{
        
        [self dismissTips];
        
        if (msg.succeed) {
            
            
            
            Status *sta = msg.GET_OUTPUT(@"status");
            if ([sta.code intValue] != 10000) {
                
                [self presentFailureTips:sta.message];
                return;
            }
            
            
            
            [self presentMessageTips:@"设置成功"];
            [[NSUserDefaults standardUserDefaults] setObject:myStyleUrl forKey:@"myShopBg"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            [self sendUISignal:self.UPDATESUCCESS withObject:myStyleUrl];
            
        }else{
            
            [self presentFailureTips:@"网络错误"];
            
        }
        
    }


}
@end




@interface DecorateBoard()
{
	//
    NSArray *mystys;
    NSString *styleid;
}
@end

@implementation DecorateBoard

SUPPORT_AUTOMATIC_LAYOUT( YES )
SUPPORT_RESOURCE_LOADING( YES )

- (void)load
{
}

- (void)unload
{
}

#pragma mark - Signal

ON_CREATE_VIEWS( signal )
{
    
    //self.MSG(HttpAPI.style).INPUT(@"body",@{@"token":[SendIFAPPDefault shareAppDefault].userToken,@"source":@"IOS",@"gradeId":@"",@"PageSize":@"100",@"pageNumber":@"1"});
    
    NSString *path = [[NSUserDefaults standardUserDefaults] objectForKey:@"myShopBg"];
    [self.bg GET:[NSString stringWithFormat:@"%@/%@",[HttpRequest sharedInstance].imageUrl,path] useCache:YES placeHolder:[UIImage imageNamed:@"sorebg"]];
    
}

ON_DELETE_VIEWS( signal )
{
}

ON_LAYOUT_VIEWS( signal )
{
}

ON_WILL_APPEAR( signal )
{
}

ON_DID_APPEAR( signal )
{
}

ON_WILL_DISAPPEAR( signal )
{
}

ON_DID_DISAPPEAR( signal )
{
}

ON_SIGNAL3(DecorateBoard, decorateclick, signal)
{

    //self.MSG(HttpAPI.shop_grade).INPUT(@"body",@{@"token":[SendIFAPPDefault shareAppDefault].userToken});
    
    self.MSG(HttpAPI.style).INPUT(@"body",@{@"token":[SendIFAPPDefault shareAppDefault].userToken,@"source":@"IOS",@"gradeId":@"",@"PageSize":@"100",@"pageNumber":@"1"});
    
    
}
ON_SIGNAL3(DecorateBoard, mystyleclick, signal)
{
    
    self.MSG(HttpAPI.style_history).INPUT(@"body",@{@"token":[SendIFAPPDefault shareAppDefault].userToken});
    
    

}
- (void)presentListView:(NSDictionary *)dataD;
{

    ShopTemplateCell *cell = [[ShopTemplateCell alloc] initWithFrame:CGRectMake(10, 100, self.size.width-20, self.view.bounds.size.height-100-64)];
    cell.data = dataD;
    cell.RELAYOUT();
    [self presentModalView:cell animated:YES transitionType:BeeUITransitionTypeFlip];
}
ON_MESSAGE3(HttpAPI, style_history, msg){

    if (msg.sending) {
        
        [self presentLoadingTips:@""];
        
    }else{
        
        [self dismissTips];
        
        if (msg.succeed) {
            
            Status *sta = msg.GET_OUTPUT(@"status");
            if ([sta.code intValue] != 10000) {
                
                [self presentFailureTips:sta.message];
                return;
            }
            
            mystys = msg.GET_OUTPUT(@"mystyles");

            [self presentListView:@{@"type":@"1",@"grades":mystys}];

            
        }else{
            
            [self presentFailureTips:@"网络错误"];
            
        }
        
    }
    


}
ON_MESSAGE3(HttpAPI, style, msg)
{
    
    if (msg.sending) {
        
        [self presentLoadingTips:@"获取风格"];
        
    }else{
        
        [self dismissTips];
        
        if (msg.succeed) {
            
            
            
            Status *sta = msg.GET_OUTPUT(@"status");
            if ([sta.code intValue] != 10000) {
                
                [self presentFailureTips:sta.message];
                return;
            }
            
            mystys = msg.GET_OUTPUT(@"mystyles");
            styleid = msg.GET_OUTPUT(@"styleid");
            
            [self presentListView:@{@"type":@"0",@"grades":mystys}];

            
            
            
        }else{
            
            [self presentFailureTips:@"网络错误"];
            
        }
        
    }
    
    
}

ON_MESSAGE3(HttpAPI, shop_grade, msg)
{
    
    if (msg.sending) {
        
        [self presentLoadingTips:@"数据获取中"];
    }else{
        
        [self dismissTips];
        if (msg.succeed) {
            
            Status *sta = msg.GET_OUTPUT(@"status");
            if ([sta.code intValue] != 10000) {
                
                [self presentMessageTips:sta.message];
                return;
            }
            //
            NSArray *shopgrades = msg.GET_OUTPUT(@"grades");
            [self presentListView:@{@"type":@"0",@"grades":shopgrades}];
            
        }else{
            
            [self presentFailureTips:@"网络错误"];
        }
        
    }
    
}

ON_SIGNAL3(DecorateBoard, back, signal)
{


    [self.stack popViewControllerAnimated:YES];
}
ON_SIGNAL3(ShopTemplateCell, UPDATESUCCESS, signal)
{

    NSString *path = [NSString stringWithFormat:@"%@/%@",[HttpRequest sharedInstance].imageUrl,signal.object];
    $(@"bg").DATA(path);

}
@end
